package com.example.springbootjavareadfileintomap.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

@RestController
@RequestMapping(value = "/home")
public class HomeController {

    @GetMapping(value = "/java-read-file-into-map-reader",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String,Object>> javaReadFileIntoMapReader(@RequestParam(value = "enumName",required = false,defaultValue = "OVERWRITE") final String enumName){
        String filePath = "";
        ClassPathResource pathResource = new ClassPathResource("/templates/theLordOfRings.txt");
        Map<String,Object> map = new HashMap<>();
        String line;
        DupKeyOption dupKey = DupKeyOption.valueOf(enumName);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(pathResource.getInputStream()))) {
            while((line = reader.readLine()) != null){
                String[] keyValuePair = line.split(":",2);
                if (keyValuePair.length > 1) {
                    String key = keyValuePair[0];
                    String value = keyValuePair[1];
                    if(DupKeyOption.OVERWRITE == dupKey){
                        map.put(key,value);
                    }else{
                        map.putIfAbsent(key,value);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @GetMapping(value = "/java-read-file-into-map-stream",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String,Object>> javaReadFileIntoMapStream(@RequestParam(value = "enum",defaultValue = "OVERWRITE",required = false) final String enumName){
        Map<String,Object> map = new HashMap<>();
        try {
            File file = ResourceUtils.getFile("classpath:templates/theLordOfRings.txt");
            DupKeyOption dupKeyOption = DupKeyOption.valueOf(enumName);
            try {
                try (Stream<String> lines = Files.lines(file.toPath())) {
                    lines.filter(line -> line.contains(":")).forEach(line ->{
                        String[] keyValuePair = line.split(":",2);
                        if (keyValuePair.length > 1) {
                            String key = keyValuePair[0];
                            String value = keyValuePair[1];
                            if (DupKeyOption.OVERWRITE == dupKeyOption) {
                                map.put(key,value);
                            }else if(DupKeyOption.DISCARD == dupKeyOption){
                                map.putIfAbsent(key,value);
                            }else{
                                map.put(key,value);
                            }
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(map,HttpStatus.OK);
    }
}
